package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.TenantMapper;
import org.example.main.entity.Tenant;
import org.example.main.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TenantServiceImpl implements TenantService {

    private static final Logger logger = Logger.getLogger(TenantServiceImpl.class);

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public int add(Tenant tenant) {
        logger.info("Adding new tenant with username: " + tenant.getUsername());
        int result = tenantMapper.insert(tenant);
        logger.info("Tenant added with ID: " + tenant.getTenantId() + ", result: " + result);
        return result;
    }

    @Override
    public int deleteById(long tenantId) {
        logger.info("Deleting tenant by ID: " + tenantId);
        int result = tenantMapper.deleteById(tenantId);
        logger.info("Tenant deletion result: " + result);
        return result;
    }

    @Override
    public int updateById(Tenant tenant) {
        logger.info("Updating tenant with ID: " + tenant.getTenantId());
        int result = tenantMapper.updateById(tenant);
        logger.info("Tenant update result: " + result);
        return result;
    }

    @Override
    public Tenant findById(long tenantId) {
        logger.info("Finding tenant by ID: " + tenantId);
        Tenant tenant = tenantMapper.findById(tenantId);
        logger.debug("Found tenant: " + tenant);
        return tenant;
    }

    @Override
    public Tenant findByUsername(String username) {
        logger.info("Finding tenant by username: " + username);
        Tenant tenant = tenantMapper.findByUsername(username);
        logger.debug("Found tenant: " + tenant);
        return tenant;
    }

    @Override
    public List<Tenant> findAll() {
        logger.info("Retrieving all tenants");
        List<Tenant> tenants = tenantMapper.findAll();
        logger.debug("Found " + tenants.size() + " tenants");
        return tenants;
    }
}
